package com.lzx.reggie.config

import com.lzx.reggie.common.JacksonObjectMapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport

@Configuration
class WebMvcConfig : WebMvcConfigurationSupport() {

    companion object {
        val log: Logger = LoggerFactory.getLogger(WebMvcConfig::class.java)
    }

    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        log.info("开始进行静态资源映射.....")
        registry.apply {
            addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/")
            addResourceHandler("/front/**").addResourceLocations("classpath:/front/")
        }

    }

    /**
     * 扩展mvc框架的消息转换器
     * @param converters
     */
    override fun extendMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
        log.info("扩展消息转换器...")

        // 将消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0,
            // 创建消息转换器对象
            MappingJackson2HttpMessageConverter().apply {
                // 设置对象转换器，底层使用Jackson将Java对象转为json
                objectMapper = JacksonObjectMapper()
            })
    }
}